package top.oylan.example.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import top.oylan.example.app.R;

// https://www.jianshu.com/p/6572ebed0d99
public class TabItem extends LinearLayout {

    private Context mContext;
    private View mView;

    private ImageView iconImg;
    private TextView textView;

    private int iconImgId;
    private String text;

    private OnClickListener onClickListener;
    private String onclickId;

    public int getIconImgId() {
        return iconImgId;
    }

    public void setIconImgId(int iconImgId) {
        if (iconImgId != 10000) {
            this.iconImgId = iconImgId;
            iconImg.setImageResource(iconImgId);
        }
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        if (text != null) {
            this.text = text;
            textView.setText(text);
        }
    }

    public String getOnclickId() {
        return onclickId;
    }

    public void setOnclickId(String onclickId) {
        this.onclickId = onclickId;
    }

    public void setViewOnClickListener(OnClickListener onlickListener) {
        this.onClickListener = onlickListener;
        mView.setOnClickListener(onlickListener);
    }

    public TabItem(Context context) {
        this(context,null);
    }

    public TabItem( Context context,AttributeSet attrs){
        this(context, attrs, 0);
    }

    public TabItem( Context context,AttributeSet attrs,int defStyleAttr){
        super(context,attrs,defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.main_tab_item, this, true);
        iconImg = mView.findViewById(R.id.tab_item_icon);
        textView = mView.findViewById(R.id.tab_item_text);
        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.TabItem);
        setText(array.getString(R.styleable.TabItem_text_content));
        setIconImgId(array.getResourceId(R.styleable.TabItem_icon_reference,10000));
    }
}
